﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using snat.EventFramework.Errors;

namespace snat.loggers
{

    public sealed class ErrorLogger
    {

        private static readonly ErrorLogger instance = new ErrorLogger();
        public static ErrorLogger Instance {
            get {
                return instance;
            }
        }

        private EventHandler<ErrorArgs> listener;
        public EventHandler<ErrorArgs> Listener {
            get {
                if (listener == null) { //remove null check if we need multiple listeners
                    listener = new EventHandler<ErrorArgs>(Instance_onError);
                }
                return listener;
            }
        }

        public string OutputFile { get; set; }
        public severity Threshold { get; set; }

        private ErrorLogger() {
            Threshold = severity.Minor; //default
        }

        

        void Instance_onError(object source, ErrorArgs args) {
            ConsoleColor old = Console.ForegroundColor;
            
            if (args.Severity > Threshold) Console.ForegroundColor = ConsoleColor.Red;
            else Console.ForegroundColor = ConsoleColor.Green;

            Console.WriteLine("\n*********************************************");
            Console.WriteLine(args.Title);
            Console.WriteLine("Severity: {0}", args.Severity.ToString());
            Console.WriteLine(args.Message);
            Console.WriteLine("*********************************************\n");

            Console.ForegroundColor = old;
        }
        


    }
}
